﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TeamBuy.AccessWebUI.Account
{
    public partial class OrderPay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Order"] != "" && Request.QueryString["Order"] != null)
            {
                txtOrderID.Text = Common.PublicFunction.DESDecrypt(Request.QueryString["Order"].ToString());

                TeamBuy.AccessService.TeamBuyOrderService getOrder = new AccessService.TeamBuyOrderService();
                getOrder.SetUserID(Convert.ToInt32(txtOrderID.Text), Convert.ToInt32(Session["UserID"]));
                DataTable dtOrder = getOrder.GetOrderInfoByIDAndUser(Convert.ToInt32(txtOrderID.Text), Convert.ToInt32(Session["UserID"]));
                if (dtOrder.Rows.Count == 1)
                {
                    txtDealID.Text = dtOrder.Rows[0]["DealID"].ToString();
                    ltlDealTitle.Text = dtOrder.Rows[0]["DealTitle"].ToString();
                    ltlPrice.Text = dtOrder.Rows[0]["NowPrice"].ToString();
                    ltlQuantity.Text = dtOrder.Rows[0]["Quantity"].ToString();
                    ltlTotal.Text = (Convert.ToInt32(dtOrder.Rows[0]["Quantity"]) * Convert.ToDouble(dtOrder.Rows[0]["NowPrice"])).ToString();
                    ltlTotalAll.Text = dtOrder.Rows[0]["TotalPrice"].ToString();
                    ltlMobileNumber.Text = dtOrder.Rows[0]["MobileNumber"].ToString();
                    ltlCardNumber.Text = dtOrder.Rows[0]["CardNumber"].ToString();
                    if (Convert.ToBoolean(dtOrder.Rows[0]["IsHadCard"]) == true)
                    {
                        cardcoderow.Visible = true;
                        TeamBuy.AccessService.TeamBuyCardService setCard = new AccessService.TeamBuyCardService();
                        DataTable dtCard = setCard.GetCardByCardNumber(ltlCardNumber.Text);
                        ltlCardNumber.Text = dtCard.Rows[0]["CardNumber"].ToString();
                        txtCardNumber.Text = dtCard.Rows[0]["CardNumber"].ToString();
                        ltlCardPrice.Text = Convert.ToDouble(dtCard.Rows[0]["CardPrice"]).ToString();
                        setCard.SetCardToUsedByCardNumber(ltlCardNumber.Text, Convert.ToInt32(Session["UserID"]));
                    }
                    else
                    {
                        cardcoderow.Visible = false;
                    }
                }
                else
                {
                    Response.Redirect("Order.aspx");
                }
            }
            else
            {
                Response.Redirect("Order.aspx");
            }
        }

        protected void btnToPay_Click(object sender, EventArgs e)
        {
            TeamBuy.AccessService.TeamBuyDealService payDeal = new AccessService.TeamBuyDealService();
            payDeal.PayDeal(Convert.ToInt32(txtDealID.Text), Convert.ToInt32(ltlQuantity.Text));
            if (txtCardNumber.Text != "")
            {
                TeamBuy.AccessService.TeamBuyCardService payCard = new AccessService.TeamBuyCardService();
                payCard.SetCardToPaiedByCardNumber(txtCardNumber.Text, Convert.ToInt32(Session["UserID"]));
            }
        }
    }
}